﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections.Specialized;
using System.ComponentModel;

namespace AppToolkit.WPF.Tests.ComponentModel
{
    [TestFixture]
    public class ObservableCollectionExTests
    {


        [Test]
        public void PropertyChanged_is_propagated()
        {
            TestClass[] items = new TestClass[]
            {
                new TestClass() { MyProperty = "Item 1" },
                new TestClass() { MyProperty = "Item 2" },
                new TestClass() { MyProperty = "Item 3" },
            };

            var target = new ObservableCollectionEx<TestClass>(items);
            
            bool itemAdded = false;

            var item = items[1];

            target.ItemChanged += (s, a) =>
                {
                    itemAdded = (a.Item == item) && (a.PropertyName == "MyProperty");
                };

            item.MyProperty = "Modified";

            Assert.IsTrue(itemAdded);
        }
        
        class TestClassParent : INotifyPropertyChanged
        {
            #region Property TestClass
            private TestClass _TestClass;

            public TestClass TestClass
            {
                get
                {
                    return _TestClass;
                }
                set
                {
                    if (_TestClass != value)
                    {
                        _TestClass = value;
                        OnPropertyChanged("TestClass");
                    }
                }
            }
            #endregion

            #region INotifyPropertyChanged Members
            protected void OnPropertyChanged(string propertyName)
            {
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            #endregion
        }

        class TestClass : INotifyPropertyChanged
        {

            #region Property MyProperty
            private string _MyProperty;

            public string MyProperty
            {
                get
                {
                    return _MyProperty;
                }
                set
                {
                    if (_MyProperty != value)
                    {
                        _MyProperty = value;
                        OnPropertyChanged("MyProperty");
                    }
                }
            }
            #endregion

            #region INotifyPropertyChanged Members
            protected void OnPropertyChanged(string propertyName)
            {
                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            #endregion
        }
    }
}
